package com.example.demo.practice;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import android.content.Intent;
import android.view.View.OnClickListener;
import android.widget.ListView;
import android.widget.Toast;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.AdapterView.OnItemClickListener;

public class UserCenter extends Activity{
    private Button leave;
    private ListView listView;
    private TextView textView;
    private List<info> mlistInfo = new ArrayList();
    private String user = "";
    private ImageView imageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.usercenter);

        textView = findViewById(R.id.update);
        textView.setOnClickListener(new textListener());

        Intent intent = getIntent();
        user = intent.getStringExtra("user");

        leave = findViewById(R.id.loginleave);
        leave.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                showTips();
            }
        });

        listView = findViewById(R.id.usermenu);
        setInfo();  //给信息赋值函数，用来测试
        listView.setAdapter(new ListViewAdapter(mlistInfo));
        //处理Item的点击事件
        listView.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                info getObject = mlistInfo.get(position);   //通过position获取所点击的对象
                String infoId = getObject.getId(); //获取信息id
                String infoTitle = getObject.getTitle();    //获取信息标题

                //Toast显示测试
                Toast.makeText(UserCenter.this, "信息ID:" + infoId, Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    protected void onStart() {
        // TODO Auto-generated method stub
        super.onStart();
    }

    public class ListViewAdapter extends BaseAdapter {
        View[] itemViews;

        public ListViewAdapter(List<info> mlistInfo) {
            // TODO Auto-generated constructor stub
            itemViews = new View[mlistInfo.size()];
            for(int i=0;i<mlistInfo.size();i++){
                info getInfo=mlistInfo.get(i);    //获取第i个对象
                //调用makeItemView，实例化一个Item
                itemViews[i]=makeItemView(
                        getInfo.getTitle(), getInfo.getText()
                );
            }
        }

        public int getCount() {
            return itemViews.length;
        }

        public View getItem(int position) {
            return itemViews[position];
        }

        public long getItemId(int position) {
            return position;
        }

        //绘制Item的函数
        private View makeItemView(String strTitle, String strText) {
            LayoutInflater inflater = (LayoutInflater) UserCenter.this
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            // 使用View的对象itemView与R.layout.item关联
            View itemView = inflater.inflate(R.layout.user_item, null);

            // 通过findViewById()方法实例R.layout.item内各组件
            TextView title = itemView.findViewById(R.id.title);
            title.setText(strTitle);    //填入相应的值
            TextView text = itemView.findViewById(R.id.text);
            text.setText(strText);

            return itemView;
        }


        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null)
                return itemViews[position];
            return convertView;
        }
    }

    public void setInfo(){
        mlistInfo.clear();
        int i=0;
        ArrayList<String> item = new ArrayList();
        item.add("用户名：");
        item.add("姓名：");
        item.add("公司：");
        item.add("联系方式：");
        ArrayList<String> text  = new ArrayList();
        text.add(user);
        text.add("李");
        text.add("123有限公司");
        text.add("100000000000");

        while(i<4){
            info information = new info();
            information.setId("menu"+i);
            information.setTitle(item.get(i));
            information.setText(text.get(i));
            mlistInfo.add(information); //将新的info对象加入到信息列表中
            i++;
        }

    }

    public class textListener implements OnClickListener {
        public void onClick(View v) {
            Intent intent = new Intent(UserCenter.this, UserManager.class);
            intent.putExtra("user",user);
            startActivity(intent);
        }
    }

    private void showTips() {
        AlertDialog alertDialog = new AlertDialog.Builder(UserCenter.this)
                .setTitle("提示:")
                .setMessage("确定退出登录?")
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        String user = "";
                        Intent intent = new Intent();
                        intent.putExtra("user",user);
                        setResult(2,intent);
                        finish();
                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        return;
                    }
                })
                .create(); //创建对话框
        alertDialog.show(); // 显示对话框
    }
}
